package com.base.library.utils;


import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    /**
     * 字符串转换成日期
     *
     * @param str
     * @return date
     */
    public static Date strToDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String getStringDateShort(Date currentTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:mm");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getStringYMD(Date currentTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String formatTime(String time) {
        if (StringUtils.isEmpty(time)) {
            return "";
        }
        return getStringDateShort(strToDate(time));
    }

    public static String getHMs(long ms) {

        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = ms / dd;
        long hour = (ms - day * dd) / hh;
        long minute = (ms - day * dd - hour * hh) / mi;
        long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        String strDay = day < 10 ? "0" + day : "" + day; //天
        String strHour = hour < 10 ? "0" + hour : "" + hour;//小时
        String strMinute = minute < 10 ? "0" + minute : "" + minute;//分钟
        String strSecond = second < 10 ? "0" + second : "" + second;//秒
        String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;//毫秒
        strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond : "" + strMilliSecond;
        return strHour + ":" + strMinute + ":" + strSecond;
    }

    public static String formatTime(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);

        Calendar curCalendar = Calendar.getInstance();
        curCalendar.setTimeInMillis(System.currentTimeMillis());

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int min = calendar.get(Calendar.MINUTE);

        long curTime = System.currentTimeMillis();
        long offset = curTime - time;
        Log.d("Time", offset + "");
        if (offset < 60 * 1000) {
            return "刚刚";
        }
        if (offset < 60 * 60 * 1000) {
            return offset / 60000 + "分钟前";
        }
        if (offset < 24 * 60 * 60 * 1000 && calendar.get(Calendar.DAY_OF_YEAR) == curCalendar.get(Calendar.DAY_OF_YEAR)) {
            return "今天" + hour + ":" + min;
        }
        if (offset < 2 * 24 * 60 * 60 * 1000 && calendar.get(Calendar.DAY_OF_YEAR) == -1 + curCalendar.get(Calendar.DAY_OF_YEAR)) {
            return "昨天" + hour + ":" + min;
        }
        if (offset < 3 * 24 * 60 * 60 * 1000 && calendar.get(Calendar.DAY_OF_YEAR) == -2 + curCalendar.get(Calendar.DAY_OF_YEAR)) {
            return "前天" + hour + ":" + min;
        }
        if (calendar.get(Calendar.YEAR) == curCalendar.get(Calendar.YEAR)) {
            return month + "-" + day + " " + hour + ":" + min;
        }
        return year + "-" + month + "-" + day;
    }
}
